package com.qdlc.p2p.biz.processor.task;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.qdlc.p2p.biz.service.BorrowRepaymentService;
import com.qdlc.p2p.biz.service.BorrowService;
import com.qdlc.p2p.common.constant.SinaConstant;
import com.qdlc.p2p.common.constant.TradeCode;
import com.qdlc.p2p.dal.dto.BorrowRepayment;
import com.qdlc.p2p.dal.dto.TppTradeLog;

/**
 * 代收/代付异步通知：处理TradeCode.C1002交易类型
 * @author linya 2016-01-13
 */
@Service
@Transactional
public class CollectionTradeNotifyC1002TaskProcessor extends AbstractCollectionTradeNotifyBaseTaskProcessor {

	private static final Logger logger = Logger.getLogger(CollectionTradeNotifyC1002TaskProcessor.class);
	
	@Resource
	BorrowRepaymentService borrowRepaymentService;
	
	@Resource
	BorrowService borrowService;
	
	/**
	 * 代付成功是否需要处理
	 * @return
	 */
	@Override
	public boolean supprotTradeFinished(String serviceType) {
		return true;
	}
	
	/**
	 * 支持的外部外部业务码
	 * @return
	 */
	public TradeCode tradeCode(){
		return TradeCode.C1002;
	}
	
	@Override
	protected void doPay(TppTradeLog tppTradeLog) {
		long borrowRepaymentId = tppTradeLog.getBorrowRepaymentId();
		BorrowRepayment borrowRepayment = borrowRepaymentService.find(borrowRepaymentId);
		if (borrowRepayment != null) {
			List<TppTradeLog> list = tppTradeService.find(borrowRepaymentId, tppTradeLog.getServiceType(),
					tppTradeLog.getTradeType());
			for (TppTradeLog log : list) {
				if (!SinaConstant.TRADE_STATUS_PAY_FINISHED.equals(log.getTradeStatus())) {
					logger.info("还款前的代收未处理完毕...");
					return;
				}
			}
			borrowService.notifySuccessRepay(borrowRepayment);
		}
	}
	
}
